/**
 * Created by Administrator on 2016/6/25.
 */
require.config({
    baseUrl:PUBLIC + "/Admin/js/lib"
});
define(function(require){
    var $ = require('jquery');
    require('jquery.form');
    require('tips/msgbox');
    window.$ = window.jQuery = $;
    var c=require('../common');
    var dialog = require('bDialog/dialog');
    window.dialog=dialog;
    require('datetimepicker/datetimepicker');
    require('select');
    require('webui');
    require('fancybox/fancybox');

    $(function() {
        //################商品管理
        //日期时间选择器
        $('#start_time').datetimepicker({
            format:'Y-m-d H:i:s',
            onShow:function( ct ){
                var end_time = $('#end_time').val();
                this.setOptions({
                    maxDate:end_time?new Date(Date.parse(end_time.replace(/-/g,   "/"))):false
                })
            },
            closeOnDateSelect:true,
            timepicker:false
        });

        $('#end_time').datetimepicker({
            format:'Y-m-d H:i:s',
            onShow:function( ct ){
                var start_time = $('#start_time').val();
                this.setOptions({
                    minDate:start_time?new Date(Date.parse(start_time.replace(/-/g,   "/"))):false
                })
            },
            closeOnDateSelect:true,
            timepicker:false
        });

        $('#shop_id').selectbox({w:175});//选择所属店铺

        //搜索展开、关闭
        $('.search_customer,.search,.cancel_search,#index_search_form,#promote_search_form').click(function(){
            $('#search_form')[0].reset();
            $('#search_form').slideToggle('fast');
        });

        /**
         * 商品列表 操作
         */
        $('select[name="order_op"]').selectbox({callback:function(v,t){
            var op = v.attr('v');
            var id = t.data('id');
            
            switch(op){
                case 'receive':
                    dialog({title:'系统提示',content:'<div style="font-size:16px;padding:20px">确定要接单？</div>',ok:function(){
                        $.post(URL+'/receive',{'id':id},function(data){
                            if(data.status == 1){
                                ZENG.msgbox.show(data.info, 4,1000);
                                setTimeout(function(){location.reload()},1000);
                            }else{
                                ZENG.msgbox.show(data.info, 5,2000);
                            }
                        },'json');
                    },cancel:function(){},okVal:'确定',cancelVal:'取消'});
                    break;
                
                case 'delivery':
                    dialog({title:'系统提示',content:'<div style="font-size:16px;padding:20px">确定配送？</div>',ok:function(){
                        $.post(URL+'/delivery',{'id':id},function(data){
                            if(data.status == 1){
                                ZENG.msgbox.show(data.info, 4,1000);
                                setTimeout(function(){location.reload()},1000);
                            }else{
                                ZENG.msgbox.show(data.info, 5,2000);
                            }
                        },'json');
                    },cancel:function(){},okVal:'确定',cancelVal:'取消'});
                    break;
                
                case 'complete':
                    dialog({title:'系统提示',content:'<div style="font-size:16px;padding:20px">确定完成？</div>',ok:function(){
                        $.post(URL+'/complete',{'id':id},function(data){
                            if(data.status == 1){
                                ZENG.msgbox.show(data.info, 4,1000);
                                setTimeout(function(){location.reload()},1000);
                            }else{
                                ZENG.msgbox.show(data.info, 5,2000);
                            }
                        },'json');
                    },cancel:function(){},okVal:'确定',cancelVal:'取消'});
                    break;
                
                case 'eval':
                    location.href = URL+'/evaluate?id='+id;
                    break;
                case 'closed':
                    dialog({title:'系统提示',content:'<div style="font-size:16px;padding:20px">确定收款？</div>',ok:function(){
                        $.post(URL+'/closed',{'id':id},function(data){
                            if(data.status == 1){
                                ZENG.msgbox.show(data.info, 4,1000);
                                setTimeout(function(){location.reload()},1000);
                            }else{
                                ZENG.msgbox.show(data.info, 5,2000);
                            }
                        },'json');
                    },cancel:function(){},okVal:'确定',cancelVal:'取消'});
                    break;
                case 'detail':
                    location.href = URL+'/detail?id='+id;
                    break;
            }
        }});

        $('a[parm]').click(function(){
            var list=$(this).attr('list');
            var lists=list.split(',');
            var arr=[];
            for(var i=0;i<lists.length;i++){
                arr.push({href :lists[i]});
            }
            $.fancybox.open(arr, {
                helpers :{thumbs:{width:75,height:50}}
            });
        });
        
        //回复订单评价（评论）
        $('.doReply').off().on('click',function () {
            var orderid = $(this).data('orderid');

            var d = dialog({title:'回复内容',content:'<textarea name="con" maxlength="200" style="width:300px;height:100px"></textarea>',ok:function(){
                var reason = $('[name="con"]').val();
                if(reason==''){
                    ZENG.msgbox.show('回复内容不能为空', 5,2000);
                    return false;
                }else{
                    $.post(URL+'/doReply',{'id':orderid,'reason':reason},function(data){
                        if(data.status == 1){
                            ZENG.msgbox.show(data.info, 4,1000);
                            setTimeout(function(){location.reload()},1000);
                        }else{
                            ZENG.msgbox.show(data.info, 5,2000);
                        }
                    },'json');
                }
            },cancel:function(){},okVal:'确定',cancelVal:'取消'});
        })
    })
});